package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployerController {
    @Autowired
    private EmployerService employerService;

    /**
     * 员工登录
     * @param employer
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employer, HttpServletRequest request){
        //对密码进行解密，md5
        String password = employer.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());

        //根据用户名在数据库中进行查找
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employer.getUsername());
        Employee emp = employerService.getOne(queryWrapper);

        //如果没有查询到则返回登录失败
        if (emp==null){
            return R.error("登录失败");
        }
        //密码比对
        if (!password.equals(emp.getPassword())){
            return R.error("登录失败");
        }

        //查看员工的状态
        if (emp.getStatus()==0){
            return R.error("账号已被禁用");
        }
        //登录成功
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清除Session中保存的员工id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
        log.info("新增员工，员工信息为{}",employee.toString());
        ///设置密码，并且使用md5进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //创建时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //创建人
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);
        employee.setCreateUser(empId);

        employerService.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("页码为{},页面大小为{},用户姓名为{}",page,pageSize,name);
       //构建分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构建条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加一个过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //进行排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employerService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);
        employerService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    @GetMapping({"/{id}"})
    public R<Employee> getId(@PathVariable Long id){
        Employee employee = employerService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到对应的员工信息");
    }
}
